//
//  PocketNewspapersViewController.m
//  suRead
//
//  Created by lidi on 15/11/10.
//  Copyright © 2015年 bao. All rights reserved.
//


#import "PocketNewspapersViewController.h"
#import "Masonry.h"
#import "UIControl+Blocks.h"
#import "ServiceNews.h"
#import "ShareService.h"

@interface PocketNewspapersViewController ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *infoWebView;
@end

@implementation PocketNewspapersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
     
    UIView *bottomBarView = [UIView new];
    [self.view addSubview:bottomBarView];
    [bottomBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        if (iPhoneX) {
            make.height.equalTo(@88.0f);
        } else {
            make.height.equalTo(@64.0f);
        }
    }];
    
    __weak typeof(self) weakSelf = self;
    UIButton *buttonWebViewBack= [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonWebViewBack setImage:[UIImage imageNamed:@"btn_refresh"] forState:UIControlStateNormal];
    [bottomBarView addSubview:buttonWebViewBack];
    [buttonWebViewBack addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        [weakSelf.infoWebView reload];
    }];
    [buttonWebViewBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(buttonWebViewBack.superview).with.offset(5.0);
        make.bottom.equalTo(buttonWebViewBack.superview);
        make.width.equalTo(@44.0f);
        make.height.equalTo(@44.0f);
    }];
    
    UIButton *buttonShare= [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonShare setImage:[UIImage imageNamed:@"icon_zb_share"] forState:UIControlStateNormal];
    [buttonShare addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        
        // 分享
        NSString *shareUrl_ = weakSelf.infoWebView.request.URL.absoluteString;
        if ([shareUrl_ rangeOfString:@"?"].location != NSNotFound)
        {
            shareUrl_ = [shareUrl_ stringByAppendingString:[NSString stringWithFormat:@"&uct=%@",[ServiceNews getAuthCode]]];
        }
        
        [ShareService share:self url:shareUrl_ title:@"楚天都市报掌报" image:@"https://ctimgs.oss-cn-hangzhou.aliyuncs.com/logo.png" completion:nil];
    }];
    [bottomBarView addSubview:buttonShare];
    [buttonShare mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(buttonShare.superview.mas_right).with.offset(-5.0);
        make.bottom.equalTo(buttonShare.superview);
        make.width.equalTo(@44.0f);
        make.height.equalTo(@44.0f);
    }];
    
    UILabel *labelTitle = [UILabel new];
    labelTitle.textColor = [UIColor blackColor];
    labelTitle.text = @"掌报";
    labelTitle.font = [UIFont fontWithName:@"FZBIAOYSJW--GB1-0" size:17.0f];
    [bottomBarView addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(labelTitle.superview);
        make.bottom.equalTo(labelTitle.superview);
        make.height.equalTo(@44.0f);
    }];
    
    UILabel *linelab = [UILabel new];
    linelab.backgroundColor = [UIColor colorWithRed:160/255.0f green:160/255.0f blue:160/255.0f alpha:1.0f];
    [bottomBarView addSubview:linelab];
    [linelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(bottomBarView);
        make.height.equalTo(@0.5f);
    }];
    
    self.infoWebView = [UIWebView new];
    self.infoWebView.scalesPageToFit = YES;
    self.infoWebView.scrollView.bounces = NO;
    self.infoWebView.delegate = self;
    [self.view addSubview:self.infoWebView];
    [self.infoWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(bottomBarView.mas_bottom);
        make.bottom.equalTo(self.view);
    }];

    NSString *source_url = @"http://ctdsb.cnhubei.com/cache/paper_ctdsb.aspx";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:source_url]];
    [self.infoWebView loadRequest:request];
}

@end
